\[ Y_i = \beta_0 + \beta_1X_i + \epsilon_i \]
\[ \hat{Y}_i = \beta_0 + \beta_1X_i \] * Error or Residual
\[ \epsilon_i = Y_i - \hat{Y}_i \]
\[ b_1 = \frac{SSXY}{SSX} \]
where
\[ SSXY = \sum_{i=1}^{n}(X_i-\bar{X})(Y_i-\bar{Y}) \]
=
\[ \frac{\sum_{i=1}^{n}(X_i)\sum_{i=1}^{n}(Y_i)}{n} \] * Intercept
\[ b_0 = \bar{Y}+b_1\bar{X} \]
\[ SST = \sum_{i=1}^{n}(Y_i-\bar{Y})^2 \]
\[ SSR = \sum_{i=1}^{n}(\hat{Y}_i-\bar{Y})^2 \]
\[ SSE = \sum_{i=1}^{n}(Y_i-\hat{Y}_i)^2 \]
\(r^2\) is always between 0 and 1 \(0 \le r^2 \le 1\)
\[ r^2 = \frac{SSR}{SST} = \]
\[ 1 - \frac{SSE}{SST} \]
\[ S_{YX} = \sqrt{\frac{SSE}{n-2}} \]
\[ S_{b_1} = \frac{S_{YX}}{\sqrt{SSX}} \]
\[ \hat{Y} \pm t_{\frac{\alpha}{2}}S_{YX}\sqrt{h_i} \]
\[ \hat{Y} \pm t_{\frac{\alpha}{2}}S_{YX}\sqrt{1+ h_i} \] where
\[ h_i = \frac{1}{n}+\frac{(X_i - \bar{X})^2}{SSX} \] \[ h_i = \frac{1}{n}+\frac{(X_i - \bar{X})^2}{\sum(X_i - \bar{X})^2} \]
\[ F_{STAT} = \frac{MSR}{MSE} \] \[ MSR = \frac{SSR}{k} \] \[ MSE = \frac{SSE}{n-k-1} \]
\(H_0\): \(\beta_1\) = 0
\(H_1\): \(\beta_1\) \(\ne\) 0
\(t_{STAT}\) = \(\frac{b_1-\beta_1}{S_{b_1}}\)
\(d.f.\) = n-2
\[ b_1 \pm t_{\frac{\alpha}{2}}S_{b_1} \] and
\[ d.f. = n-2 \]
\(H_0\): \(\rho\) = 0
\(H_1\): \(\rho\) \(\ne\) 0
\(t_{STAT}\) = \(\frac{r - \rho}{\sqrt{\frac{1-r^2}{n-2}}}\)
\(d.f.\) = n-2
\(r\) = + \(\sqrt{r^2}\) if \(b_1 > 0\) or \(r\) = - \(\sqrt{r^2}\) if \(b_1 < 0\)
#install.packages("UsingR")
library(UsingR)
## Warning: package 'UsingR' was built under R version 4.1.2
## Loading required package: MASS
## Warning: package 'MASS' was built under R version 4.1.2
## Loading required package: HistData
## Loading required package: Hmisc
## Warning: package 'Hmisc' was built under R version 4.1.2
## Loading required package: lattice
## Loading required package: survival
## Warning: package 'survival' was built under R version 4.1.2
## Loading required package: Formula
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
##
## Attaching package: 'UsingR'
## The following object is masked from 'package:survival':
##
## cancer
## Load Data Set
data(galton)
## Create a Scatter Plot
plot(galton$parent,galton$child,pch=19,col="blue")
## Conduct a linear Model Regression
lm1 <- lm(galton$child~galton$parent)
## Add Regression Line to the scatter plot
lines(galton$parent, lm1$fitted, col="black",lwd=3)
## Create Sample Record
newGalton <- data.frame(parent=rep(NA,1e6), child=rep(NA,1e6))
## Verify Record
head(newGalton)
## parent child
## 1 NA NA
## 2 NA NA
## 3 NA NA
## 4 NA NA
## 5 NA NA
## 6 NA NA
## Add Parent Records
newGalton$parent <- rnorm(1e6,mean=mean(galton$parent), sd=sd(galton$parent))
## Add Child Record
newGalton$child <- lm1$coeff[1] + lm1$coeff[2]*newGalton$parent + rnorm(1e6,sd=sd(lm1$residuals))
## Verify New Data Set
head(newGalton)
## parent child
## 1 66.33289 65.50516
## 2 65.85542 66.14508
## 3 66.16100 71.81595
## 4 68.22543 66.81991
## 5 67.67480 66.96356
## 6 68.91756 69.74420
## Visualize
smoothScatter(newGalton$parent,newGalton$child)
abline(lm1,col="red",lwd=3)
## Start Taking Samples:
set.seed(12345)
sampleGalton1 <- newGalton[sample(1:1e6,50,replace=F),]
plot(sampleGalton1,pch=19,col="blue")
sampleLm1 <- lm(sampleGalton1$child~sampleGalton1$parent)
lines(sampleGalton1$parent,sampleLm1$fitted,lwd=3,lty=2)
abline(lm1,col="red",lwd=3)
## Take another Sample
sampleGalton2 <- newGalton[sample(1:1e6,50,replace=F),]
plot(sampleGalton2,pch=19,col="blue")
sampleLm2 <- lm(sampleGalton2$child~sampleGalton2$parent)
lines(sampleGalton1$parent,sampleLm1$fitted,lwd=3,lty=2)
lines(sampleGalton2$parent,sampleLm2$fitted,lwd=3,lty=3)
abline(lm1,col="red",lwd=3)
## Take many samples
sampleLm <- vector(100,mode="list")
for(i in 1:100){ sampleGalton <- newGalton[sample(1:1e6,50,replace=F),];sampleLm[[i]]<- lm(sampleGalton$child~sampleGalton$parent)}
##n Visualize
smoothScatter(newGalton$parent,newGalton$child)
for(i in 1:100){abline(sampleLm[[i]],lwd=3,lty=2)}
abline(lm1,col="red",lwd=4)
## Check Histogram of the Coefficients
par(mfrow=c(1,2))
hist(sapply(sampleLm,function(x){coef(x)[1]}),col="blue",xlab="Intercept",main="")
hist(sapply(sampleLm,function(x){coef(x)[2]}),col="blue",xlab="Slope",main="")
summary(sampleLm2)$coeff
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.9371244 8.2120274 3.645522 6.550151e-04
## sampleGalton2$parent 0.5589164 0.1203513 4.644041 2.678252e-05
confint(sampleLm2,level=0.95)
## 2.5 % 97.5 %
## (Intercept) 13.4257366 46.4485121
## sampleGalton2$parent 0.3169339 0.8008989
## End of First Regression!!!!
# Get and load library FPP
library("fpp")
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 4.1.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: fma
##
## Attaching package: 'fma'
## The following object is masked from 'package:UsingR':
##
## chicken
## The following objects are masked from 'package:MASS':
##
## cement, housing, petrol
## Loading required package: expsmooth
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.1.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.1.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 4.1.2
# Loop at all the variables
plot(fuel[,-1])
pairs(fuel[,-1])
summary(fuel)
## Model Cylinders Litres
## Volvo S60 FWD : 3 Min. :4.000 Min. :1.30
## Chevrolet Colorado 2WD : 2 1st Qu.:4.000 1st Qu.:2.00
## Chevrolet Colorado Crew Cab 2WD: 2 Median :4.000 Median :2.40
## Chevrolet Malibu : 2 Mean :4.157 Mean :2.31
## Chrysler PT Cruiser : 2 3rd Qu.:4.000 3rd Qu.:2.50
## Dodge Caliber : 2 Max. :5.000 Max. :3.70
## (Other) :121
## Barrels City Highway Cost
## Min. : 7.40 Min. :15.00 Min. :20.00 Min. : 615
## 1st Qu.:13.20 1st Qu.:19.00 1st Qu.:27.00 1st Qu.:1091
## Median :14.30 Median :21.00 Median :28.00 Median :1182
## Mean :14.31 Mean :21.97 Mean :28.89 Mean :1185
## 3rd Qu.:15.60 3rd Qu.:23.75 3rd Qu.:31.00 3rd Qu.:1290
## Max. :20.10 Max. :48.00 Max. :45.00 Max. :1667
##
## Carbon
## Min. : 4.000
## 1st Qu.: 7.100
## Median : 7.700
## Mean : 7.671
## 3rd Qu.: 8.300
## Max. :10.800
##
cor(fuel[,-1])
## Cylinders Litres Barrels City Highway Cost
## Cylinders 1.0000000 0.5072599 0.3612070 -0.3246731 -0.2863089 0.3615217
## Litres 0.5072599 1.0000000 0.6795817 -0.5228023 -0.6663893 0.6796274
## Barrels 0.3612070 0.6795817 1.0000000 -0.9062413 -0.9288892 0.9999567
## City -0.3246731 -0.5228023 -0.9062413 1.0000000 0.8209571 -0.9052234
## Highway -0.2863089 -0.6663893 -0.9288892 0.8209571 1.0000000 -0.9284566
## Cost 0.3615217 0.6796274 0.9999567 -0.9052234 -0.9284566 1.0000000
## Carbon 0.3633336 0.6798169 0.9994928 -0.9079411 -0.9267689 0.9994937
## Carbon
## Cylinders 0.3633336
## Litres 0.6798169
## Barrels 0.9994928
## City -0.9079411
## Highway -0.9267689
## Cost 0.9994937
## Carbon 1.0000000
# Assumption of Normality
hist(fuel$Carbon)
# Assumption of Linearity
plot(Carbon ~ City, data = fuel)
# Lets do a plot of two variables
plot(jitter(Carbon) ~ jitter(City), xlab="City (mpg)", ylab="Carbon footprint (tonnes per year)", data=fuel)
# Simple Linear Regression
fit <- lm(Carbon ~ City, data=fuel)
fit
##
## Call:
## lm(formula = Carbon ~ City, data = fuel)
##
## Coefficients:
## (Intercept) City
## 12.526 -0.221
summary(fit)
##
## Call:
## lm(formula = Carbon ~ City, data = fuel)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.7014 -0.3643 -0.1062 0.1938 2.0809
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.525647 0.199232 62.87 <2e-16 ***
## City -0.220970 0.008878 -24.89 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4703 on 132 degrees of freedom
## Multiple R-squared: 0.8244, Adjusted R-squared: 0.823
## F-statistic: 619.5 on 1 and 132 DF, p-value: < 2.2e-16
anova(fit)
## Analysis of Variance Table
##
## Response: Carbon
## Df Sum Sq Mean Sq F value Pr(>F)
## City 1 137.005 137.005 619.52 < 2.2e-16 ***
## Residuals 132 29.191 0.221
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Draw the regression line in the plot abline(fit)
# Lets do a residual plot and see what it looks like
res <- residuals(fit)
plot(jitter(res)~jitter(City), ylab="Residuals", xlab="City", data=fuel)
abline(0,0)
# Lets do some plots to see what R gives for regression plot(fit)
# On with Forecasting to see the output from regression
fitted(fit)[1]
## 20
## 7.001388
# ask to do a forecast for predictor = 30
fcast <- forecast(fit, newdata=data.frame(City=30))
# plot the forecast to see the graph
plot(fcast, xlab="City (mpg)", ylab="Carbon footprint (tons per year)")
# Lets ask for some confidence interval for our forecast
confint(fit,level=0.95)
## 2.5 % 97.5 %
## (Intercept) 12.1315464 12.9197478
## City -0.2385315 -0.2034092
# Lets do a transformation of response and predictors to get a better fit.
# Split the plotting output area into 2 columns and 1 row.
par(mfrow=c(1,2))
# Do a Regression on log of the data.
fit2 <- lm(log(Carbon) ~ log(City), data=fuel)
# Plot 1
plot(jitter(Carbon) ~ jitter(City), xlab="City (mpg)",
ylab="Carbon footprint (tonnes per year)", data=fuel)
# Add a line based on the exponents of the fit variables
lines(1:50, exp(fit2$coef[1]+fit2$coef[2]*log(1:50)))
# Plot 2
plot(log(jitter(Carbon)) ~ log(jitter(City)),
xlab="log City mpg", ylab="log carbon footprint", data=fuel)
#Draw the regression line
abline(fit2)
# Look at the residuals.
#Store the residuals in a variable
res <- residuals(fit2)
# Lets so the residuals a little bigger
par(mfrow=c(1,1))
plot(jitter(res, amount=.005) ~ jitter(log(City)),
ylab="Residuals", xlab="log(City)", data=fuel)
# End of Simple Linear Regression Exercise
# Multiple Linear Regression Example View(credit)
# Get Correlation Matrix
cor(credit)
## score savings income fte single
## score 1.00000000 0.40440532 0.31005351 0.05934809 -0.09301482
## savings 0.40440532 1.00000000 0.13754620 -0.03330092 -0.08252345
## income 0.31005351 0.13754620 1.00000000 0.12486839 -0.35237334
## fte 0.05934809 -0.03330092 0.12486839 1.00000000 0.07021355
## single -0.09301482 -0.08252345 -0.35237334 0.07021355 1.00000000
## time.address 0.15033646 0.02273133 -0.01311510 0.05959628 0.03527295
## time.employed 0.17972354 -0.04713921 0.06951179 -0.04203080 -0.07679786
## time.address time.employed
## score 0.15033646 0.17972354
## savings 0.02273133 -0.04713921
## income -0.01311510 0.06951179
## fte 0.05959628 -0.04203080
## single 0.03527295 -0.07679786
## time.address 1.00000000 0.05336579
## time.employed 0.05336579 1.00000000
# Look at the correlation visually
pairs(credit[,-(4:5)])
# Plot reveals that we need to do a transformation. As some values are zero we will do log +1
#Lets create a new variable with the logs.
creditlog <- data.frame(score=credit$score, log.savings=log(credit$savings+1), log.income=log(credit$income+1), log.address=log(credit$time.address+1), log.employed=log(credit$time.employed+1), fte=credit$fte, single=credit$single)
# Now look at correlation
pairs(creditlog[,1:5])
# Lets do Multiple Regression and do a fit
mfit <- step(lm(score ~ log.savings + log.income + log.address + log.employed + single, data=creditlog))
## Start: AIC=2325.38
## score ~ log.savings + log.income + log.address + log.employed +
## single
##
## Df Sum of Sq RSS AIC
## - single 1 40.6 51132 2323.8
## <none> 51091 2325.4
## - log.employed 1 1052.9 52144 2333.6
## - log.income 1 1306.9 52398 2336.0
## - log.address 1 3914.8 55006 2360.3
## - log.savings 1 29555.3 80647 2551.6
##
## Step: AIC=2323.78
## score ~ log.savings + log.income + log.address + log.employed
##
## Df Sum of Sq RSS AIC
## <none> 51132 2323.8
## - log.employed 1 1063.7 52196 2332.1
## - log.income 1 1666.2 52798 2337.8
## - log.address 1 3891.0 55023 2358.4
## - log.savings 1 29515.1 80647 2549.6
summary(mfit)
##
## Call:
## lm(formula = score ~ log.savings + log.income + log.address +
## log.employed, data = creditlog)
##
## Residuals:
## Min 1Q Median 3Q Max
## -26.133 -6.966 -1.125 5.379 37.446
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.2186 5.2309 -0.042 0.96668
## log.savings 10.3526 0.6124 16.904 < 2e-16 ***
## log.income 5.0521 1.2579 4.016 6.83e-05 ***
## log.address 2.6666 0.4345 6.137 1.72e-09 ***
## log.employed 1.3138 0.4094 3.209 0.00142 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.16 on 495 degrees of freedom
## Multiple R-squared: 0.4701, Adjusted R-squared: 0.4658
## F-statistic: 109.8 on 4 and 495 DF, p-value: < 2.2e-16
# Lets plot the original with the predicted.
plot(fitted(mfit), creditlog$score, ylab="Score", xlab="Predicted score")
# Lets analyse Residuals.
# Lets do 2 rows two columns for our plot
par(mfrow=c(2,2))
plot(creditlog$log.savings,residuals(mfit),xlab="log(savings)")
plot(creditlog$log.income,residuals(mfit),xlab="log(income)")
plot(creditlog$log.address,residuals(mfit),xlab="log(address)")
plot(creditlog$log.employed,residuals(mfit),xlab="log(employed)")
# One more plot of fitted and residuals
# Back to one column one row for big plots
par(mfrow=c(1,1))
plot(fitted(mfit), residuals(mfit), xlab="Predicted scores", ylab="Residuals")
# Plot gives multiple graphs for regression analysis
plot(mfit)
# More Outlier measures
cooks.distance(mfit)
## 1 2 3 4 5 6
## 4.051051e-04 1.729785e-03 8.311866e-05 3.142736e-03 7.907947e-04 3.908466e-04
## 7 8 9 10 11 12
## 8.083366e-04 7.103351e-04 7.205655e-04 1.868111e-04 1.165014e-04 4.075184e-04
## 13 14 15 16 17 18
## 2.307393e-04 4.178442e-03 1.731300e-05 4.910764e-04 1.477818e-03 4.064875e-04
## 19 20 21 22 23 24
## 9.164389e-04 1.095508e-03 1.192237e-03 9.956692e-05 3.541666e-03 1.268629e-04
## 25 26 27 28 29 30
## 8.527395e-04 4.725106e-07 8.527052e-03 6.393144e-04 3.341921e-03 1.259718e-04
## 31 32 33 34 35 36
## 2.712541e-04 2.744761e-04 1.417882e-04 1.969763e-03 4.050582e-05 2.715615e-04
## 37 38 39 40 41 42
## 6.195392e-02 3.418682e-03 2.200256e-03 3.495179e-03 1.008214e-05 9.067949e-06
## 43 44 45 46 47 48
## 8.818308e-04 3.381141e-04 1.774477e-03 1.165606e-04 1.025643e-03 8.186106e-04
## 49 50 51 52 53 54
## 4.321428e-03 2.600804e-05 1.572226e-03 2.477833e-03 1.303792e-05 5.590926e-06
## 55 56 57 58 59 60
## 3.376027e-05 3.860727e-04 2.189096e-03 1.047668e-03 2.212494e-03 1.954115e-03
## 61 62 63 64 65 66
## 1.929580e-04 3.627637e-03 1.741508e-04 5.325141e-04 2.833372e-06 9.183896e-04
## 67 68 69 70 71 72
## 3.849327e-03 2.171143e-03 7.715362e-03 7.354513e-05 3.424648e-04 4.726752e-05
## 73 74 75 76 77 78
## 1.558689e-04 5.572702e-05 5.080879e-04 7.023998e-05 2.440131e-03 2.718839e-04
## 79 80 81 82 83 84
## 3.899619e-03 1.167024e-03 2.851297e-03 2.782971e-03 3.483400e-02 2.542610e-03
## 85 86 87 88 89 90
## 4.298478e-05 7.847650e-04 1.727130e-03 9.583978e-04 6.037729e-03 9.596773e-05
## 91 92 93 94 95 96
## 5.071991e-05 4.416070e-03 7.765058e-03 8.085673e-04 8.279679e-04 1.900803e-05
## 97 98 99 100 101 102
## 1.383593e-04 3.962356e-04 1.035818e-04 4.334311e-03 2.032776e-03 1.955998e-05
## 103 104 105 106 107 108
## 4.289720e-04 1.348355e-04 2.191473e-02 7.168164e-05 3.838467e-06 1.423444e-03
## 109 110 111 112 113 114
## 3.754928e-04 1.040292e-03 9.054161e-03 3.909952e-03 3.533724e-04 2.415057e-04
## 115 116 117 118 119 120
## 5.092441e-03 8.745124e-04 3.140147e-04 2.951805e-04 1.640536e-06 3.345009e-04
## 121 122 123 124 125 126
## 6.635401e-06 2.747851e-04 1.319360e-04 8.570672e-04 6.809195e-05 1.101615e-04
## 127 128 129 130 131 132
## 7.708570e-06 7.732614e-04 8.827675e-03 1.828411e-03 5.365437e-04 2.919913e-04
## 133 134 135 136 137 138
## 1.030097e-04 1.555994e-03 5.400540e-03 2.785334e-03 1.165650e-02 1.135483e-03
## 139 140 141 142 143 144
## 2.328636e-04 7.599702e-04 7.855314e-06 3.999202e-04 2.344337e-03 1.398212e-02
## 145 146 147 148 149 150
## 1.705616e-04 8.797003e-04 1.020859e-03 6.509040e-05 1.658737e-03 1.493473e-04
## 151 152 153 154 155 156
## 6.843117e-04 1.182026e-03 2.135121e-05 1.453412e-03 1.152734e-04 7.123579e-03
## 157 158 159 160 161 162
## 7.260600e-03 2.048427e-07 1.754778e-04 4.525302e-04 5.379148e-04 1.657840e-03
## 163 164 165 166 167 168
## 1.632431e-05 2.692196e-03 8.640181e-04 4.387531e-04 8.485658e-09 6.824317e-04
## 169 170 171 172 173 174
## 8.465303e-05 4.701519e-03 1.920932e-03 1.558927e-04 3.716776e-03 1.842968e-03
## 175 176 177 178 179 180
## 3.475816e-02 1.593831e-03 1.845392e-03 6.032396e-05 7.063502e-04 3.775858e-03
## 181 182 183 184 185 186
## 2.771270e-03 1.454387e-05 2.703894e-04 4.728581e-06 1.028516e-03 4.577951e-02
## 187 188 189 190 191 192
## 3.376428e-03 4.168602e-03 3.439168e-04 2.725357e-05 2.408123e-05 1.527414e-04
## 193 194 195 196 197 198
## 1.709611e-03 9.195786e-06 5.527057e-04 2.585964e-04 7.561515e-04 2.254806e-03
## 199 200 201 202 203 204
## 4.097967e-03 1.434187e-04 5.987985e-05 2.167177e-04 7.819290e-03 1.807388e-04
## 205 206 207 208 209 210
## 1.954493e-03 4.925005e-03 1.450325e-04 2.186163e-02 7.310055e-03 2.261924e-02
## 211 212 213 214 215 216
## 1.197311e-03 1.191412e-03 1.554084e-03 1.260414e-04 9.435582e-04 4.802544e-05
## 217 218 219 220 221 222
## 9.173002e-06 1.359986e-05 5.652554e-06 1.453128e-04 7.728299e-03 3.489421e-04
## 223 224 225 226 227 228
## 2.664422e-04 1.936458e-03 1.200312e-03 1.583171e-02 1.334945e-03 5.801202e-03
## 229 230 231 232 233 234
## 2.769853e-04 6.322407e-03 5.206445e-02 1.318283e-03 5.509224e-04 5.210090e-06
## 235 236 237 238 239 240
## 8.670782e-05 1.631937e-03 2.554565e-05 3.143729e-03 5.320888e-03 1.149965e-03
## 241 242 243 244 245 246
## 2.624935e-03 1.505782e-03 3.435900e-04 5.926477e-04 4.792521e-06 1.998259e-03
## 247 248 249 250 251 252
## 1.089049e-03 1.837863e-04 2.643252e-05 5.062546e-03 6.153077e-04 2.647678e-03
## 253 254 255 256 257 258
## 2.999714e-04 2.128694e-04 1.709659e-03 1.623825e-03 4.160121e-05 1.190050e-04
## 259 260 261 262 263 264
## 2.783962e-03 4.220669e-04 5.012555e-03 3.502698e-03 2.745375e-04 8.206397e-04
## 265 266 267 268 269 270
## 1.894728e-03 7.045750e-04 1.214010e-04 2.322880e-04 4.884182e-04 9.829011e-04
## 271 272 273 274 275 276
## 2.017807e-05 3.784961e-05 2.941640e-04 6.498525e-04 6.209947e-04 6.882779e-05
## 277 278 279 280 281 282
## 6.751263e-04 1.145093e-03 2.973867e-04 3.041667e-04 1.744043e-03 6.187570e-06
## 283 284 285 286 287 288
## 1.780876e-04 6.173502e-04 2.932442e-04 8.600843e-05 9.345685e-04 4.963507e-04
## 289 290 291 292 293 294
## 2.567321e-03 3.617525e-04 2.500105e-04 1.046003e-03 7.693488e-04 1.307944e-03
## 295 296 297 298 299 300
## 5.377909e-04 9.337509e-05 5.232381e-04 1.127785e-03 1.296490e-03 1.336911e-03
## 301 302 303 304 305 306
## 4.945008e-05 3.089426e-03 3.597263e-04 1.665548e-03 3.179473e-03 1.500078e-03
## 307 308 309 310 311 312
## 9.377717e-05 1.630285e-04 2.538308e-04 1.232696e-04 1.541966e-03 2.781060e-06
## 313 314 315 316 317 318
## 6.429360e-04 4.141661e-05 1.100340e-04 7.756757e-03 6.266110e-03 3.146947e-03
## 319 320 321 322 323 324
## 2.986093e-03 5.038954e-04 1.678454e-04 3.968006e-05 2.429374e-05 4.332314e-04
## 325 326 327 328 329 330
## 2.518175e-04 2.402268e-03 1.258893e-03 1.127178e-03 6.687371e-03 3.745045e-03
## 331 332 333 334 335 336
## 7.010791e-04 1.054133e-02 1.847345e-05 3.226082e-04 6.133326e-04 4.134558e-04
## 337 338 339 340 341 342
## 1.558257e-04 2.823642e-03 3.310190e-04 5.547554e-04 1.958648e-05 5.767271e-03
## 343 344 345 346 347 348
## 8.728290e-03 1.423261e-06 2.484673e-04 7.953327e-06 9.617142e-05 4.544587e-04
## 349 350 351 352 353 354
## 1.015887e-04 3.501337e-03 1.135030e-02 5.433935e-04 4.292583e-03 9.389302e-04
## 355 356 357 358 359 360
## 5.949221e-03 1.061641e-04 1.450082e-03 1.220245e-03 3.878350e-04 3.145547e-03
## 361 362 363 364 365 366
## 3.270204e-03 1.508866e-02 1.031074e-02 1.574193e-04 6.435982e-04 1.247757e-04
## 367 368 369 370 371 372
## 5.660949e-03 8.792416e-03 1.338518e-03 2.009602e-04 1.230509e-04 1.395403e-03
## 373 374 375 376 377 378
## 7.776009e-05 1.352619e-02 1.588136e-02 1.416125e-05 9.505888e-03 1.764746e-04
## 379 380 381 382 383 384
## 2.026535e-02 2.866143e-04 8.227437e-05 6.651736e-05 2.862637e-04 3.188983e-04
## 385 386 387 388 389 390
## 6.201179e-03 6.071435e-05 5.265106e-05 9.453468e-05 5.591786e-04 2.708232e-02
## 391 392 393 394 395 396
## 4.357551e-02 3.125491e-05 1.692657e-04 3.212243e-03 7.497023e-04 3.152622e-05
## 397 398 399 400 401 402
## 1.590388e-03 7.799167e-04 4.709193e-05 1.443526e-03 1.788712e-07 2.274145e-03
## 403 404 405 406 407 408
## 5.804156e-04 5.817042e-04 1.171026e-03 8.107716e-05 7.909289e-04 7.574811e-04
## 409 410 411 412 413 414
## 9.446013e-06 1.688684e-04 2.114634e-05 2.665259e-03 1.207304e-05 3.646793e-03
## 415 416 417 418 419 420
## 9.237106e-04 3.162309e-03 5.602221e-04 2.031118e-05 1.719697e-03 2.524083e-03
## 421 422 423 424 425 426
## 3.008282e-03 1.253466e-04 1.444209e-03 2.602724e-04 1.400520e-04 4.347142e-04
## 427 428 429 430 431 432
## 1.107302e-03 1.148027e-02 1.319521e-02 3.680071e-06 5.061113e-04 4.493698e-04
## 433 434 435 436 437 438
## 1.895808e-03 1.107297e-03 9.803797e-03 1.497241e-04 4.605079e-04 2.152513e-04
## 439 440 441 442 443 444
## 2.944386e-05 1.612511e-03 3.362744e-04 2.884537e-03 4.733477e-05 1.090125e-05
## 445 446 447 448 449 450
## 9.589350e-05 5.936347e-03 4.982653e-05 9.679593e-05 2.857653e-04 3.566635e-03
## 451 452 453 454 455 456
## 3.253391e-02 1.121974e-03 5.118650e-05 4.217875e-03 2.827604e-04 1.339141e-04
## 457 458 459 460 461 462
## 1.046473e-03 2.620268e-08 8.125263e-03 1.991486e-03 7.178070e-05 5.918874e-04
## 463 464 465 466 467 468
## 1.273682e-04 4.135719e-03 2.853847e-04 3.173735e-04 2.458840e-03 1.313956e-03
## 469 470 471 472 473 474
## 5.780277e-05 1.071080e-03 4.774297e-06 6.695363e-04 4.092412e-04 1.927520e-04
## 475 476 477 478 479 480
## 3.077767e-04 2.124336e-05 5.195115e-03 8.677694e-03 1.980321e-04 3.224312e-04
## 481 482 483 484 485 486
## 5.963074e-03 2.610010e-03 1.600034e-03 1.997672e-03 2.804816e-04 3.014607e-06
## 487 488 489 490 491 492
## 4.418145e-05 3.321042e-04 1.240100e-03 1.013186e-03 1.473349e-04 6.932471e-04
## 493 494 495 496 497 498
## 5.236953e-04 7.933896e-04 5.775743e-04 8.511641e-04 4.371039e-05 5.185947e-04
## 499 500
## 7.585227e-04 9.712721e-04
hatvalues(mfit)
## 1 2 3 4 5 6
## 0.013549287 0.005533546 0.008275536 0.003277853 0.008287643 0.004841357
## 7 8 9 10 11 12
## 0.003580605 0.006764909 0.006690287 0.004820021 0.006746627 0.013207949
## 13 14 15 16 17 18
## 0.009341717 0.005459476 0.003944172 0.022001529 0.008614707 0.020825451
## 19 20 21 22 23 24
## 0.024624657 0.007853395 0.004189364 0.004011857 0.013943151 0.005279961
## 25 26 27 28 29 30
## 0.007278029 0.010518195 0.016522072 0.006085409 0.011215737 0.004154521
## 31 32 33 34 35 36
## 0.005575054 0.006055264 0.010168984 0.009292679 0.003072573 0.002304438
## 37 38 39 40 41 42
## 0.042918505 0.012423115 0.007861509 0.015122035 0.009675679 0.015868614
## 43 44 45 46 47 48
## 0.005541902 0.015648840 0.004449012 0.004312333 0.009728066 0.007404931
## 49 50 51 52 53 54
## 0.027369611 0.005507429 0.010207787 0.008305879 0.008676827 0.011736780
## 55 56 57 58 59 60
## 0.004177757 0.006378689 0.005442759 0.013360041 0.009299951 0.003646747
## 61 62 63 64 65 66
## 0.018263033 0.007807814 0.006386336 0.005983582 0.004193610 0.014889241
## 67 68 69 70 71 72
## 0.030635730 0.010726449 0.007761921 0.004285364 0.007396787 0.016814831
## 73 74 75 76 77 78
## 0.008558472 0.007169741 0.004075364 0.005967758 0.009915403 0.007116486
## 79 80 81 82 83 84
## 0.012955256 0.006165736 0.004763520 0.006517307 0.017983905 0.004147247
## 85 86 87 88 89 90
## 0.006221637 0.005615599 0.017720440 0.004432751 0.017797040 0.003781632
## 91 92 93 94 95 96
## 0.006189212 0.005277586 0.012047497 0.011684771 0.019231965 0.002931971
## 97 98 99 100 101 102
## 0.006070866 0.005771692 0.007366416 0.015766572 0.004642524 0.005605395
## 103 104 105 106 107 108
## 0.009529855 0.003399468 0.009906385 0.012655299 0.014251882 0.014545166
## 109 110 111 112 113 114
## 0.009149451 0.007866390 0.003313011 0.014676418 0.005166948 0.007012010
## 115 116 117 118 119 120
## 0.006408809 0.006688242 0.024042080 0.014396780 0.005469329 0.004447327
## 121 122 123 124 125 126
## 0.008306883 0.010147812 0.008722320 0.012275340 0.003962253 0.008433943
## 127 128 129 130 131 132
## 0.005774947 0.012383190 0.018724941 0.008999210 0.009477288 0.006344975
## 133 134 135 136 137 138
## 0.006303371 0.015034033 0.005390477 0.005443776 0.022620035 0.006803268
## 139 140 141 142 143 144
## 0.011827431 0.016903852 0.003657262 0.004161823 0.007617547 0.009966052
## 145 146 147 148 149 150
## 0.009555097 0.007040490 0.005557434 0.005184403 0.007029120 0.008548986
## 151 152 153 154 155 156
## 0.003773020 0.038859119 0.015376674 0.008058520 0.006985657 0.009753832
## 157 158 159 160 161 162
## 0.020273086 0.013518197 0.005719993 0.003995290 0.008114050 0.011914744
## 163 164 165 166 167 168
## 0.003504814 0.031704771 0.009186174 0.008746894 0.007604521 0.008864814
## 169 170 171 172 173 174
## 0.020686459 0.007986693 0.006640899 0.008723107 0.012241344 0.004792426
## 175 176 177 178 179 180
## 0.016889379 0.007138168 0.005780097 0.009013190 0.005723565 0.020854744
## 181 182 183 184 185 186
## 0.010880441 0.006992715 0.004050242 0.009027107 0.010189438 0.059033495
## 187 188 189 190 191 192
## 0.020444284 0.009865370 0.007556495 0.003114736 0.004905710 0.003005777
## 193 194 195 196 197 198
## 0.008608843 0.007019496 0.006227746 0.007717805 0.004310056 0.007502887
## 199 200 201 202 203 204
## 0.007704636 0.004654252 0.009232956 0.007337941 0.016970456 0.004335310
## 205 206 207 208 209 210
## 0.009086970 0.007707000 0.004613985 0.022416442 0.012189916 0.010247388
## 211 212 213 214 215 216
## 0.004449329 0.009681078 0.016251369 0.008265622 0.007973695 0.004719761
## 217 218 219 220 221 222
## 0.017666285 0.004530755 0.003710460 0.006735481 0.009132436 0.007915835
## 223 224 225 226 227 228
## 0.008158760 0.005544232 0.004848946 0.042956432 0.004038641 0.013780066
## 229 230 231 232 233 234
## 0.006484066 0.014256791 0.018930697 0.009487834 0.010238706 0.015884138
## 235 236 237 238 239 240
## 0.005563522 0.006179603 0.011172211 0.009276987 0.008659431 0.005346606
## 241 242 243 244 245 246
## 0.015954141 0.004241114 0.017106752 0.005314971 0.002725421 0.006657994
## 247 248 249 250 251 252
## 0.007271747 0.012927397 0.009681741 0.011962197 0.008714474 0.010417292
## 253 254 255 256 257 258
## 0.005124933 0.003878998 0.008794482 0.008904300 0.005698989 0.009920229
## 259 260 261 262 263 264
## 0.005559650 0.004399023 0.006612674 0.009986594 0.007055918 0.006359001
## 265 266 267 268 269 270
## 0.004036095 0.003950984 0.006385511 0.008670387 0.007617414 0.003874705
## 271 272 273 274 275 276
## 0.005213672 0.005011292 0.004909178 0.003465050 0.007103864 0.005818190
## 277 278 279 280 281 282
## 0.004284623 0.008948348 0.009996440 0.004701196 0.006634092 0.016774505
## 283 284 285 286 287 288
## 0.006178054 0.003936398 0.010867483 0.005744833 0.007542923 0.005412542
## 289 290 291 292 293 294
## 0.007965899 0.014169840 0.008180637 0.005703621 0.004583747 0.006877021
## 295 296 297 298 299 300
## 0.019869757 0.004531595 0.019115197 0.008665590 0.006834021 0.012256809
## 301 302 303 304 305 306
## 0.009124936 0.007949045 0.004249845 0.010404923 0.009216320 0.006677807
## 307 308 309 310 311 312
## 0.004405537 0.005923429 0.008536047 0.007009244 0.007168952 0.003940313
## 313 314 315 316 317 318
## 0.004345075 0.005277856 0.005314482 0.017434886 0.021079591 0.007680251
## 319 320 321 322 323 324
## 0.010806961 0.013944878 0.007649566 0.023827642 0.007258786 0.010346457
## 325 326 327 328 329 330
## 0.009846838 0.036979370 0.011250579 0.006049176 0.010326927 0.014199390
## 331 332 333 334 335 336
## 0.004803179 0.030586120 0.007402210 0.007906887 0.008860450 0.005649074
## 337 338 339 340 341 342
## 0.004122687 0.011916705 0.007793461 0.005682006 0.006145419 0.009262543
## 343 344 345 346 347 348
## 0.027858954 0.004911526 0.007084146 0.004553217 0.007207065 0.005187095
## 349 350 351 352 353 354
## 0.007928047 0.021903171 0.035048239 0.008718513 0.027008109 0.007543925
## 355 356 357 358 359 360
## 0.010517870 0.003131693 0.032032583 0.004842367 0.010579480 0.020561609
## 361 362 363 364 365 366
## 0.007085815 0.023944033 0.012862678 0.006069949 0.005948138 0.005253157
## 367 368 369 370 371 372
## 0.013206871 0.004638247 0.015918347 0.010814470 0.004159656 0.007065616
## 373 374 375 376 377 378
## 0.005382047 0.037759459 0.009235761 0.003252706 0.014015037 0.004636161
## 379 380 381 382 383 384
## 0.021496628 0.003022836 0.006127400 0.005923398 0.010409399 0.004551968
## 385 386 387 388 389 390
## 0.019167899 0.018961970 0.007829671 0.007695704 0.024392840 0.021932645
## 391 392 393 394 395 396
## 0.024129134 0.003717459 0.004922214 0.018689358 0.009805939 0.006888865
## 397 398 399 400 401 402
## 0.009872677 0.006561849 0.005611843 0.005707463 0.007251774 0.006272782
## 403 404 405 406 407 408
## 0.008832765 0.011010763 0.010666405 0.004921823 0.011037377 0.003839243
## 409 410 411 412 413 414
## 0.015945135 0.009615193 0.002579398 0.013711094 0.008291139 0.009941848
## 415 416 417 418 419 420
## 0.011565889 0.015703399 0.031145502 0.003509472 0.008008397 0.012458793
## 421 422 423 424 425 426
## 0.008560414 0.003886298 0.007295455 0.015527392 0.011379947 0.004562744
## 427 428 429 430 431 432
## 0.012144682 0.007804892 0.027327041 0.012024145 0.005549796 0.005382785
## 433 434 435 436 437 438
## 0.006746420 0.014223385 0.012667765 0.005118210 0.023394486 0.016279756
## 439 440 441 442 443 444
## 0.008548915 0.004945220 0.003758112 0.015012651 0.014629007 0.011895734
## 445 446 447 448 449 450
## 0.009004568 0.011431527 0.016419347 0.008033024 0.008517971 0.012301981
## 451 452 453 454 455 456
## 0.018096806 0.012703645 0.002525320 0.008293851 0.004875099 0.005429912
## 457 458 459 460 461 462
## 0.003780246 0.016445476 0.020382582 0.022715924 0.004364339 0.003979695
## 463 464 465 466 467 468
## 0.004222459 0.005291461 0.005749860 0.017427273 0.013494739 0.007424092
## 469 470 471 472 473 474
## 0.013008650 0.007163236 0.006570245 0.003638937 0.009304590 0.014742078
## 475 476 477 478 479 480
## 0.003519339 0.002368021 0.008507498 0.020630183 0.007751670 0.007790989
## 481 482 483 484 485 486
## 0.028734611 0.006178132 0.024743541 0.011385560 0.012428093 0.005315830
## 487 488 489 490 491 492
## 0.004247147 0.016466570 0.009005512 0.004502619 0.006243791 0.013343254
## 493 494 495 496 497 498
## 0.005856979 0.012074787 0.010920007 0.017134803 0.003181510 0.024643230
## 499 500
## 0.028510555 0.009515486